<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目类型')" prop="dealType">
            <dict-select v-model="form.dealType" dictionary="deal_type" :placeholder="$t('项目类型')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('归档目录')" prop="directoryId">
            <treeselect
              v-model="form.directoryId"
              :options="defaultDir"
              :flat="true"
              :multiple="false"
              :placeholder="$t('归档目录')"
              :normalizer="checkFileNormalizer"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.id">
        <div class="area-header">
          <span class="area-header-title">{{ $t('目录配置') }}</span>
        </div>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['dd:template:edit']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="addHandle(null)"
            >{{ $t('新增') }}</el-button>
          </el-col>
        </el-row>
        <el-table
          v-loading="loading"
          :data="form.dirList"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          default-expand-all
          :tree-props="{children: 'children', hasChildren: true}"
        >
          <el-table-column :label="$t('文档类型')" align="left" prop="dirName" fixed="left" />
          <el-table-column :label="$t('备注')" align="center" prop="remark" />
          <el-table-column :label="$t('归档目录')" align="center" prop="directoryName" />
          <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['dd:template:edit']"
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="updateHandle(scope.row)"
              >{{ $t('修改') }}</el-button>
              <el-button
                v-if="scope.row.parentId === '0'"
                v-hasPermi="['dd:template:edit']"
                size="mini"
                type="text"
                icon="el-icon-plus"
                @click="addHandle(scope.row)"
              >{{ $t('新增') }}</el-button>
              <el-button
                v-hasPermi="['dd:template:remove']"
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="deleteHandle(scope.row)"
              >{{ $t('删除') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="dd:template" />
    </div>
    <!-- 添加或修改DataRoom配置子表对话框 -->
    <el-dialog :title="$t('添加目录')" :visible.sync="formOpen" width="30%" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <ddTemplateDirForm ref="ddTemplateDirForm" />
    </el-dialog>
  </div>
</template>

<script>
import { delDdTemplateDir } from '@/api/dd/template/ddTemplateDir'
import { saveDdTemplate, getDdTemplate, getDirList } from '@/api/dd/template/ddTemplate'
import Treeselect from '@riophae/vue-treeselect'
import { leftListSysMenuConfigList } from '@/api/file/filefolder/folder.js'
import ddTemplateDirForm from './ddTemplateDirForm.vue'
export default {
  components: {
    Treeselect,
    ddTemplateDirForm
  },
  data() {
    return {
      // 关联的文档库类型
      defaultDir: [],
      // 表单参数
      form: {
        dirList: []
      },
      formOpen: false,
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        dealType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        directoryId: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      },
      dirListCheck: {
        dirName: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
      }
    }
  },
  created() {
    this.getDicts('deal_type').then(response => {
      this.dealTypeOptions = response.data
    })
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDdTemplate(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
      // 查询默认的文件夹
      this.getDefaultDir()
    },
    refreshDirList() {
      getDirList(this.form.id).then(response => {
          this.form.dirList = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealType: undefined,
        directoryId: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        dirList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDdTemplate(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.form = response.data
              // this.closeForm()
              this.loading = false
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    },
    // 获取选填附件内容
    getDefaultDir() {
      leftListSysMenuConfigList({ 'sysRoleId': '1' }).then(res => {
        res.data.forEach(item => {
          this.checkFileNormalizer(item)
        })
        this.defaultDir = res.data
      })
    },
    // 格式化方法
    checkFileNormalizer(node) {
      if (!node.children || (node.children && node.children.length === 0)) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    /** 新增按钮操作 */
    addHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            if (row) {
                this.$refs.ddTemplateDirForm.init({ 'id': undefined, 'parentId': row.id, 'templateId': this.form.id })
              } else {
                this.$refs.ddTemplateDirForm.init({ 'id': undefined, 'parentId': '0', 'templateId': this.form.id })
              }
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.ddTemplateDirForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDdTemplateDir(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    }
  }
}
</script>
